package com.intellagric.service.travel_business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.dao.product.ProductDao;
import com.intellagric.dao.travel_business.TravelBusinessDao;
import com.intellagric.interfaces.travel_business.TravelBusinessService;
import com.intellagric.mapper.TravelBusinessMapper;
import com.intellagric.pojo.TravelBusiness;
import com.intellagric.pojo.TravelBusinessExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TravelBusinessServiceImpl implements TravelBusinessService {
    @Autowired
    private TravelBusinessMapper travelBusinessMapper;

    @Autowired
    private TravelBusinessDao travelBusinessDao;

    @Autowired
    private ProductDao productDao;

    @Override
    public ResponseMessage addTravelBusiness(TravelBusiness travelBusiness) {


        if(travelBusinessMapper.insertSelective(travelBusiness)==1)
        {
            return ResponseMessage.success();
         } else {
        return ResponseMessage.fail();
        }

    }

    @Override
    public ResponseMessage deleteTravelBusinessById(String id) {
        if (travelBusinessMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        }else {
            return ResponseMessage.fail();
        }

    }

    @Override
    public ResponseMessage batchDelete(String[] ids) {


        if (travelBusinessDao.batchDeleteByPrimaryKey(ids) <1)
            return ResponseMessage.fail();
        return ResponseMessage.success();
    }


    @Override
    public ResponseMessage editTravelBusiness(TravelBusiness travelBusiness) {

        if (travelBusinessMapper.updateByPrimaryKeySelective(travelBusiness) == 1) {
            return ResponseMessage.success();
        }
        return ResponseMessage.fail();
    }

    @Override
    public TravelBusiness getTravelBusinessById(String id) {

        TravelBusiness travelBusiness=travelBusinessMapper.selectByPrimaryKey(id);

        return travelBusiness;
    }

    @Override
    public com.intellagric.common.bean.LayuiDataGridResult getTravelBusinessList(int page, int rows,String keyword) {
        PageHelper.startPage(page, rows);
        TravelBusinessExample travelBusinessExample = new TravelBusinessExample();
        if(keyword!=null&&keyword!="")
            travelBusinessExample.createCriteria().andNameLike("%"+keyword+"%");
        else
            travelBusinessExample.createCriteria().andIdIsNotNull();

        List<TravelBusiness> travelBusinesses = travelBusinessMapper.selectByExample(travelBusinessExample);
        PageInfo info=new PageInfo(travelBusinesses);
        return com.intellagric.common.bean.LayuiDataGridResult.success().add(travelBusinesses,info.getTotal());
    }

    @Override
    public List<Map<String, String>> getComsumption() {
        Calendar calendar=Calendar.getInstance();
        List<Map<String,String>> results=new ArrayList<>();

        for (int i=0;i<6;i++){
            Map<String ,String> map=new HashMap<>();
            String year=String.valueOf(calendar.get(Calendar.YEAR));
            String month=String.valueOf(calendar.get(Calendar.MONTH)+1);
            String count=String.valueOf(productDao.getSum(year,month)+travelBusinessDao.getSum(year,month));

            map.put("value",count);
            map.put("name",year+"-"+month);
            results.add(map);
            calendar.add(Calendar.MONTH,-1);
        }
        return results;
    }


}
